package com.blogspot.mkorwel.jaas;

import java.io.Serializable;
import java.security.Principal;
import java.security.acl.Group;
import java.util.Collections;
import java.util.Enumeration;
import java.util.HashSet;
import java.util.Set;

/**
 * 
 * @author mkorwel
 *
 */
public class MyGroup implements Group, Serializable {

	/**
	 * 
	 */
	private static final long serialVersionUID = -3978735590793434379L;
	private final String name;
	private final Set<Principal> users = new HashSet<Principal>();

	public MyGroup(String name) {
		this.name = name;
	}

	public boolean addMember(Principal user) {
		return users.add(user);
	}

	public boolean removeMember(Principal user) {
		return users.remove(user);
	}

	public boolean isMember(Principal member) {
		return users.contains(member);
	}

	public Enumeration<? extends Principal> members() {
		return Collections.enumeration(users);
	}

	public String getName() {
		return name;
	}

	public boolean equals(Object o) {
		if (o == null)
			return false;

		if (this == o)
			return true;

		if (!(o instanceof MyGroup))
			return false;
		MyGroup that = (MyGroup) o;

		if (this.getName().equals(that.getName()))
			return true;
		return false;
	}

	public int hashCode() {
		return name.hashCode();
	}

}
